import 'package:number_spark_flutter/config/ns_base_controller.dart';
import 'package:number_spark_flutter/model/ns_user_info_model.dart';
import 'package:number_spark_flutter/network/ns_api.dart';
import 'package:number_spark_flutter/network/ns_network_utils.dart';
import 'package:number_spark_flutter/pages/ns_login/view/ns_login_agreement_dialog.dart';
import 'package:number_spark_flutter/routes/ns_app_pages.dart';
import 'package:number_spark_flutter/utils/ns_current_limiting_util.dart';
import 'package:number_spark_flutter/utils/ns_log_report_utils.dart';
import 'package:number_spark_flutter/utils/ns_user_utils.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter_udid/flutter_udid.dart';
import 'package:get/get.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';

class NSLoginLogic extends NSBaseController {
  bool agreePrivateStatus = false;

  void doClickAppleLogin() async {
    if (!agreePrivateStatus) {
      Get.dialog(NSLoginAgreementDialog(
        doClickAgree: () {
          Get.back();
          agreePrivateStatus = true;
          update();
          NSCurrentLimitingUtil.debounce(() {
            onAppleLogin();
          });
        },
      ));
      return;
    }
    NSLogReportUtils.stat(page: NSLogPages.nsAppLogin);
    NSCurrentLimitingUtil.debounce(() {
      onAppleLogin();
    });
  }

  doClickFastLogin() async {
    if (!agreePrivateStatus) {
      Get.dialog(NSLoginAgreementDialog(
        doClickAgree: () {
          Get.back();
          agreePrivateStatus = true;
          update();
          NSCurrentLimitingUtil.debounce(() {
            onFastLogin();
          });
        },
      ));
      return;
    }
    NSLogReportUtils.stat(page: NSLogPages.nsQuickLogin);
    NSCurrentLimitingUtil.debounce(() {
      onFastLogin();
    });
  }

  onAppleLogin() async {
    final credential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
    );
    Map<String, dynamic> params = {'oauthType': 3, 'token': credential.identityToken};
    NSResponseData responseData = await NSApi.userLogin.post(params, isJson: false);
    if (responseData.isSuccess()) {
      onLoginSuccess(responseData);
    }
  }

  onFastLogin() async {
    BotToast.showLoading();
    Map<String, dynamic> params = {'oauthType': 4, 'token': await FlutterUdid.udid};
    // Get.offAndToNamed(Routes.main);
    NSResponseData responseData = await NSApi.userLogin.post(params, isJson: false);
    if (responseData.isSuccess()) {
      onLoginSuccess(responseData);
    }
    BotToast.closeAllLoading();
  }

  onLoginSuccess(NSResponseData responseData) async {
    NSNetWorkUtils.setAccessToken(responseData.data['token']);
    NSUserInfoModel userInfoModel = NSUserInfoModel.fromJson(responseData.data['userInfo']);
    userInfoModel.token = responseData.data['token'];
    NSUserUtils.saveUserInfo(userInfoModel);
    Get.offAndToNamed(NSRoutes.main);
  }

  doClickSelect() {
    agreePrivateStatus = !agreePrivateStatus;
    update();
  }
}
